﻿using IPC.Communication.Core.Types;
using IPC.Communication.Profinet.Siemens;
using IPC.Communication;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IPC.ListConnect.ListProprety;
using IPC.ListConnect.Profinet;
using Newtonsoft.Json;
using Newtonsoft;
using Newtonsoft.Json.Linq;
using IPC.DbMapper;

namespace IPC.ListConnect.Connect
{
    public class VirtualConnect:BaseConnect
    {
        #region 构造函数
        public VirtualConnect(string connectName, string ip, int port, int slot, int rack) : base(connectName, ip, port, slot, rack)
        {
            IsOpen = true;
        }
        #endregion

        #region 读

        public override OperateResult Read()
        {
            return base.Read();
            //string channelName = base.ConnectName.Split('.')[0];
            //string equipName = base.ConnectName.Split(".")[1];
            //string equipFullName = GetEquipFullName(channelName, equipName);
            //OperateResult operateResult = new OperateResult();
            //operateResult.IsSuccess = false;
            //operateResult.Message = "未找到标签";
            //foreach (var item in EquipChannels.Instance.EquipChannelList)
            //{
            //    if (item.ChannelName == channelName)
            //    {
            //        bool b = false;
            //        foreach (var itemTag in item.Equips[equipFullName].Tags.Values)
            //        {
            //            switch (itemTag.ReadWrite)
            //            {
            //                case ReadWrite.Read:
            //                    itemTag.RealValue = itemTag.RealValue == "未知" ? "0" : itemTag.RealValue;
            //                    itemTag.IsSuccess = true;
            //                    itemTag.Result = "成功";
            //                    operateResult.IsSuccess = true;
            //                    operateResult.Message = "成功";
            //                    break;
            //                case ReadWrite.ReadWrite:
            //                    itemTag.RealValue = itemTag.RealValue == "未知" ? "0" : itemTag.RealValue;
            //                    itemTag.IsSuccess = true;
            //                    itemTag.Result = "成功";
            //                    operateResult.IsSuccess = true;
            //                    operateResult.Message = "成功";
            //                    break;
            //                case ReadWrite.Write:
            //                    itemTag.RealValue = "不允许读";
            //                    itemTag.IsSuccess = false;
            //                    itemTag.Result = "不允许读";
            //                    operateResult.IsSuccess = false;
            //                    operateResult.Message = "不允许读";
            //                    break;
            //            }
            //        }

            //        foreach (var groups in item.Equips[equipFullName].Groups.Values)
            //        {
            //            foreach (var itemTag in groups.Tags.Values)
            //            {
            //                switch (itemTag.ReadWrite)
            //                {
            //                    case ReadWrite.Read:
            //                        itemTag.RealValue = itemTag.RealValue == "未知" ? "0" : itemTag.RealValue;
            //                        itemTag.IsSuccess = true;
            //                        itemTag.Result = "成功";
            //                        operateResult.IsSuccess = true;
            //                        operateResult.Message = "成功";
            //                        break;
            //                    case ReadWrite.ReadWrite:
            //                        itemTag.Value = itemTag.RealValue == "未知" ? "0" : itemTag.RealValue;
            //                        operateResult.IsSuccess = true;
            //                        operateResult.Message = "成功";
            //                        break;
            //                    case ReadWrite.Write:
            //                        itemTag.Value = "不允许读";
            //                        operateResult.IsSuccess = false;
            //                        operateResult.Message = "不允许读";
            //                        break;
            //                }
            //            }
            //        }
            //        break;
            //    }
            //}
            //return operateResult;
        }

        /// <summary>
        /// 按组读 返回json
        /// </summary>
        /// <param name="equipChannel"></param>
        /// <param name="equip"></param>
        /// <param name="group"></param>
        public override string ReadJson(string channelName, string equip, string groupName = "")
        {
            base.Read(equip, groupName);

            string equipFullName = GetEquipFullName(channelName, equip);
            string groupFullName = GetGroupFullName(channelName, equip, groupName);
            OperateResult operateResult = new OperateResult();
            operateResult.IsSuccess = false;
            operateResult.Message = "未找到标签";
            ReadResult readResult = new ReadResult();
            readResult.IsSuccess = false;
            readResult.Message = "未找到标签";
            foreach (var item in EquipChannels.Instance.EquipChannelList)
            {
                if (item.ChannelName == channelName)
                {
                    if (!item.Equips.ContainsKey(equipFullName))
                    {
                        operateResult.IsSuccess = false;
                        operateResult.Message = $"未找到{equipFullName}设备";
                        readResult.IsSuccess = operateResult.IsSuccess;
                        readResult.Message = operateResult.Message;
                        return JsonConvert.SerializeObject(readResult);
                    }

                    if (!JudgeHaveEquipOrGroup(item, equip, groupName, out operateResult))
                    {
                        readResult.IsSuccess = operateResult.IsSuccess;
                        readResult.Message = operateResult.Message;
                        return JsonConvert.SerializeObject(readResult);
                    }


                    if (groupName == "")
                    {
                        foreach (var itemTag in item.Equips[equipFullName].Tags.Values)
                        {
                            switch (itemTag.ReadWrite)
                            {
                                case ReadWrite.Read:
                                    itemTag.Value = itemTag.Value == "未知" ? "0" : itemTag.Value;
                                    operateResult.IsSuccess = true;
                                    operateResult.Message = "成功";
                                    readResult.IsSuccess = true;
                                    readResult.Message = "成功";
                                    readResult.Result.Add(itemTag.Name, itemTag.Value);
                                    break;
                                case ReadWrite.ReadWrite:
                                    itemTag.Value = itemTag.Value == "未知" ? "0" : itemTag.Value;
                                    operateResult.IsSuccess = true;
                                    operateResult.Message = "成功";
                                    readResult.IsSuccess = true;
                                    readResult.Message = "成功";
                                    readResult.Result.Add(itemTag.Name, itemTag.Value);
                                    break;
                                case ReadWrite.Write:
                                    itemTag.Value = "不允许读";
                                    operateResult.IsSuccess = false;
                                    operateResult.Message = "不允许读";
                                    readResult.IsSuccess = false;
                                    readResult.Message = "不允许读";
                                    readResult.Result.Add(itemTag.Name, "不允许读");
                                    break;
                            }
                        }
                    }
                    else
                    {
                        foreach (var itemTag in item.Equips[equipFullName].Groups[groupFullName].Tags.Values)
                        {
                            switch (itemTag.ReadWrite)
                            {
                                case ReadWrite.Read:
                                    itemTag.Value = itemTag.Value == "未知" ? "0" : itemTag.Value;
                                    operateResult.IsSuccess = true;
                                    operateResult.Message = "成功";
                                    readResult.IsSuccess = true;
                                    readResult.Message = "成功";
                                    readResult.Result.Add(itemTag.Name, itemTag.Value);
                                    break;
                                case ReadWrite.ReadWrite:
                                    itemTag.Value = itemTag.Value == "未知" ? "0" : itemTag.Value;
                                    operateResult.IsSuccess = true;
                                    operateResult.Message = "成功";
                                    readResult.IsSuccess = true;
                                    readResult.Message = "成功";
                                    readResult.Result.Add(itemTag.Name, itemTag.Value);
                                    break;
                                case ReadWrite.Write:
                                    itemTag.Value = "不允许读";
                                    operateResult.IsSuccess = false;
                                    operateResult.Message = "不允许读";
                                    readResult.IsSuccess = false;
                                    readResult.Message = "不允许读";
                                    readResult.Result.Add(itemTag.Name, "不允许读");
                                    break;
                            }
                        }
                    }
                    break;
                }
            }
            return JsonConvert.SerializeObject(readResult);
        }

        /// <summary>
        /// 单点读 返回json
        /// </summary>
        /// <param name="channelName"></param>
        /// <param name="equipName"></param>
        /// <param name="tagName"></param>
        /// <param name="groupName"></param>
        public override string ReadJson(string channelName, string equipName, string tagName, string groupName = "")
        {
            base.Read(channelName, equipName, tagName, groupName);
            string equipFullName = GetEquipFullName(channelName, equipName);
            string groupFullName = GetGroupFullName(channelName, equipName, groupName);
            OperateResult operateResult = new OperateResult();
            operateResult.IsSuccess = false;
            operateResult.Message = "未找到标签";
            ReadResult readResult = new ReadResult();
            readResult.IsSuccess = false;
            readResult.Message = "未找到标签";
            foreach (var itemChannel in EquipChannels.Instance.EquipChannelList)
            {
                if (channelName == itemChannel.ChannelName)
                {
                    if (!JudgeHaveEquipOrGroup(itemChannel, equipName, groupName, out operateResult))
                    {
                        readResult.IsSuccess = operateResult.IsSuccess;
                        readResult.Message = operateResult.Message;
                        return JsonConvert.SerializeObject(readResult);
                    }
                    if (groupName == "")
                    {
                        foreach (var itemTag in itemChannel.Equips[equipFullName].Tags.Values)
                        {
                            if (itemTag.Name == tagName)
                            {
                                switch (itemTag.ReadWrite)
                                {
                                    case ReadWrite.Read:
                                        operateResult.IsSuccess = true;
                                        operateResult.Message = "成功";
                                        itemTag.Value = itemTag.Value == "未知" ? "0" : itemTag.Value;
                                        readResult.IsSuccess = true;
                                        readResult.Message = "成功";
                                        readResult.Result.Add(itemTag.Name, itemTag.Value);
                                        break;
                                    case ReadWrite.ReadWrite:
                                        operateResult.IsSuccess = true;
                                        operateResult.Message = "成功";
                                        itemTag.Value = itemTag.Value == "未知" ? "0" : itemTag.Value;
                                        readResult.IsSuccess = true;
                                        readResult.Message = "成功";
                                        readResult.Result.Add(itemTag.Name, itemTag.Value);
                                        break;
                                    case ReadWrite.Write:
                                        operateResult.IsSuccess = false;
                                        operateResult.Message = "不允许读";
                                        itemTag.Value = "不允许读";
                                        readResult.IsSuccess = false;
                                        readResult.Message = "不允许读";
                                        readResult.Result.Add(itemTag.Name, itemTag.Value);
                                        break;
                                }
                                break;
                            }
                        }
                        break;
                    }
                    else
                    {
                        foreach (var itemTag in itemChannel.Equips[equipFullName].Groups[groupFullName].Tags.Values)
                        {
                            if (itemTag.Name == tagName)
                            {
                                switch (itemTag.ReadWrite)
                                {
                                    case ReadWrite.Read:
                                        operateResult.IsSuccess = true;
                                        operateResult.Message = "成功";
                                        itemTag.Value = itemTag.Value == "未知" ? "0" : itemTag.Value;
                                        readResult.IsSuccess = true;
                                        readResult.Message = "成功";
                                        readResult.Result.Add(itemTag.Name, itemTag.Value);
                                        break;
                                    case ReadWrite.ReadWrite:
                                        operateResult.IsSuccess = true;
                                        operateResult.Message = "成功";
                                        itemTag.Value = itemTag.Value == "未知" ? "0" : itemTag.Value;
                                        readResult.IsSuccess = true;
                                        readResult.Message = "成功";
                                        readResult.Result.Add(itemTag.Name, itemTag.Value);
                                        break;
                                    case ReadWrite.Write:
                                        operateResult.IsSuccess = false;
                                        operateResult.Message = "不允许读";
                                        itemTag.Value = "不允许读";
                                        readResult.IsSuccess = false;
                                        readResult.Message = "不允许读";
                                        readResult.Result.Add(itemTag.Name, itemTag.Value);
                                        break;
                                }
                                break;
                            }
                        }
                        break;
                    }
                }
            }
            return JsonConvert.SerializeObject(readResult);
        }
        #endregion

        #region 写
        /// <summary>
        /// 单点写
        /// </summary>
        /// <param name="channelName"></param>
        /// <param name="equipName"></param>
        /// <param name="tagName"></param>
        /// <param name="values"></param>
        /// <param name="groupName"></param>
        /// <returns></returns>
        public override OperateResult Write(string channelName, string equipName, string tagName, string values, string groupName = "")
        {
            base.Write(channelName, equipName, tagName, values, groupName);
            string equipFullName = GetEquipFullName(channelName, equipName);
            string groupFullName = GetGroupFullName(channelName, equipName, groupName);
            OperateResult operateResult = new OperateResult();
            operateResult.IsSuccess = false;
            operateResult.Message = "未找到点";
            
            try
            {
                foreach (var itemChannel in EquipChannels.Instance.EquipChannelList)
                {
                    if(itemChannel.ChannelName == channelName)
                    {
                        if (!JudgeHaveEquipOrGroup(itemChannel, equipName, groupName, out operateResult))
                        {
                            
                            return operateResult;
                        }
                        if (groupName == "")
                        {
                            foreach (var item in itemChannel.Equips[equipFullName].Tags.Values)
                            {
                                if (item.Name == tagName)
                                {
                                    switch (item.ReadWrite)
                                    {
                                        case ReadWrite.Read:
                                            operateResult.IsSuccess = false;
                                            operateResult.Message = "不允许写";
                                            return operateResult;
                                    }
                                    item.RealValue = values;
                                    operateResult.IsSuccess = true;
                                    operateResult.Message = "成功";
                                    return operateResult;
                                }
                            }
                            break;
                        }
                        else
                        {
                            foreach (var item in itemChannel.Equips[equipFullName].Groups[groupFullName].Tags.Values)
                            {
                                if (item.Name == tagName)
                                {
                                    switch (item.ReadWrite)
                                    {
                                        case ReadWrite.Read:
                                            operateResult.IsSuccess = false;
                                            operateResult.Message = "不允许写";
                                            return operateResult;
                                    }
                                    item.RealValue = values;
                                    operateResult.IsSuccess = true;
                                    operateResult.Message = "成功";
                                    return operateResult;
                                }
                            }
                            break;
                        }
                    }
                }

                operateResult.IsSuccess = false;
                operateResult.Message = "未找到点";
            }
            catch (Exception ex)
            {
                operateResult.IsSuccess = false;
                operateResult.Message = ex.Message;
            }
            return operateResult;
        }

        /// <summary>
        /// 按组写
        /// </summary>
        /// <param name="channelName"></param>
        /// <param name="equipName"></param>
        /// <param name="values"></param>
        /// <param name="startTag"></param>
        /// <param name="groupName"></param>
        /// <returns></returns>
        public override OperateResult Write(string channelName, string equipName, string[] values, string startTag = "", string groupName = "")
        {
            base.Write(channelName, equipName, values, startTag, groupName);
            OperateResult operateResult = new OperateResult();
            operateResult.IsSuccess = false;
            operateResult.Message = "未找到点";
            string equipFullName = GetEquipFullName(channelName, equipName);
            string groupFullName = GetGroupFullName(channelName, equipName, groupName);
            try
            {
                foreach (var itemChannel in EquipChannels.Instance.EquipChannelList)
                {
                    if (itemChannel.ChannelName == channelName)
                    {
                        if (!JudgeHaveEquipOrGroup(itemChannel, equipName, groupName, out operateResult))
                        {
                            return operateResult;
                        }
                        if (groupName == "")
                        {
                            if (startTag != "")
                            {
                                bool writeFlag = false;
                                int j = 0;
                                foreach (var itemTag in itemChannel.Equips[equipFullName].Tags.Values)
                                {
                                    if (itemTag.Name == startTag)
                                    {
                                        writeFlag = true;
                                    }
                                    if (writeFlag)
                                    {
                                        if (!ArrayEnough(values, j))
                                        {
                                            break;
                                        }
                                        operateResult.IsSuccess = true;
                                        switch (itemTag.ReadWrite)
                                        {
                                            case ReadWrite.Read:
                                                operateResult.IsSuccess = false;
                                                operateResult.Message += $"{itemTag.FullName} 不允许写";
                                                break;
                                            default:
                                                itemTag.RealValue = values[j];
                                                break;
                                        }
                                        j++;
                                    }
                                }

                                if (!writeFlag)
                                {
                                    operateResult.IsSuccess = false;
                                    operateResult.Message = "未找到起始点，请确认是否存在";
                                }
                            }
                            else
                            {
                                int j = 0;
                                foreach (var itemTag in itemChannel.Equips[equipFullName].Tags.Values)
                                {
                                    if (!ArrayEnough(values, j))
                                    {
                                        break;
                                    }
                                    operateResult.IsSuccess = true;
                                    switch (itemTag.ReadWrite)
                                    {
                                        case ReadWrite.Read:
                                            operateResult.IsSuccess = false;
                                            operateResult.Message += $"{itemTag.FullName} 不允许写";
                                            break;
                                        default:
                                            itemTag.RealValue = values[j];
                                            break;
                                    }
                                    j++;
                                }
                            }
                        }
                        else
                        {
                            if (startTag != "")
                            {
                                bool writeFlag = false;
                                int j = 0;
                                foreach (var itemTag in itemChannel.Equips[equipFullName].Groups[groupFullName].Tags.Values)
                                {
                                    if (itemTag.Name == startTag)
                                    {
                                        writeFlag = true;
                                    }
                                    if (writeFlag)
                                    {
                                        if (!ArrayEnough(values, j))
                                        {
                                            break;
                                        }
                                        operateResult.IsSuccess = true;
                                        switch (itemTag.ReadWrite)
                                        {
                                            case ReadWrite.Read:
                                                operateResult.IsSuccess = false;
                                                operateResult.Message += $"{itemTag.FullName} 不允许写";
                                                break;
                                            default:
                                                itemTag.RealValue = values[j];
                                                break;
                                        }
                                        j++;
                                    }
                                }
                            }
                            else
                            {
                                int j = 0;
                                foreach (var itemTag in itemChannel.Equips[equipFullName].Groups[groupFullName].Tags.Values)
                                {
                                    if (!ArrayEnough(values, j))
                                    {
                                        break;
                                    }
                                    operateResult.IsSuccess = true;
                                    switch (itemTag.ReadWrite)
                                    {
                                        case ReadWrite.Read:
                                            operateResult.IsSuccess = false;
                                            operateResult.Message += $"{itemTag.FullName} 不允许写";
                                            break;
                                        default:
                                            itemTag.RealValue = values[j];
                                            break;
                                    }
                                    j++;
                                }
                            }
                        }
                        break;
                    }
                }
                return operateResult;
            }
            catch (Exception ex)
            {
                operateResult.IsSuccess = false;
                operateResult.Message = ex.Message;
            }
            return operateResult;
        }

        /// <summary>
        /// 按组写
        /// </summary>
        /// <param name="channelName"></param>
        /// <param name="equipName"></param>
        /// <param name="values"></param>
        /// <param name="startTag"></param>
        /// <param name="groupName"></param>
        /// <returns></returns>
        public override string WriteJson(string channelName, string equipName, string[] values, string startTag = "", string groupName = "")
        {
            base.Write(channelName, equipName, values, startTag, groupName);
            OperateResult operateResult = new OperateResult();
            operateResult.IsSuccess = false;
            operateResult.Message = "未找到点";
            string equipFullName = GetEquipFullName(channelName, equipName);
            string groupFullName = GetGroupFullName(channelName, equipName, groupName);
            ReadResult readResult = new ReadResult();
            readResult.IsSuccess = false;
            readResult.Message = "未找到标签";
            try
            {
                foreach (var itemChannel in EquipChannels.Instance.EquipChannelList)
                {
                    if (itemChannel.ChannelName == channelName)
                    {
                        if (!JudgeHaveEquipOrGroup(itemChannel, equipName, groupName, out operateResult))
                        {
                            readResult.IsSuccess = operateResult.IsSuccess;
                            readResult.Message = operateResult.Message;
                            return JsonConvert.SerializeObject(readResult);
                        }
                        if (groupName == "")
                        {
                            if (startTag != "")
                            {
                                bool writeFlag = false;
                                int j = 0;
                                foreach (var itemTag in itemChannel.Equips[equipFullName].Tags.Values)
                                {
                                    if (itemTag.Name == startTag)
                                    {
                                        writeFlag = true;
                                    }
                                    if (writeFlag)
                                    {
                                        if (!ArrayEnough(values, j))
                                        {
                                            break;
                                        }
                                        operateResult.IsSuccess = true;
                                        switch (itemTag.ReadWrite)
                                        {
                                            case ReadWrite.Read:
                                                operateResult.IsSuccess = false;
                                                operateResult.Message += $"{itemTag.FullName} 不允许写";
                                                readResult.IsSuccess = operateResult.IsSuccess;
                                                readResult.Message = operateResult.Message;
                                                break;
                                            default:
                                                itemTag.RealValue = values[j];
                                                break;
                                        }
                                        j++;
                                    }
                                }
                            }
                            else
                            {
                                int j = 0;
                                foreach (var itemTag in itemChannel.Equips[equipFullName].Tags.Values)
                                {
                                    if (!ArrayEnough(values, j))
                                    {
                                        break;
                                    }
                                    operateResult.IsSuccess = true;
                                    switch (itemTag.ReadWrite)
                                    {
                                        case ReadWrite.Read:
                                            operateResult.IsSuccess = false;
                                            operateResult.Message += $"{itemTag.FullName} 不允许写";
                                            readResult.IsSuccess = operateResult.IsSuccess;
                                            readResult.Message = operateResult.Message;
                                            break;
                                        default:
                                            itemTag.RealValue = values[j];
                                            break;
                                    }
                                    j++;
                                }
                            }
                        }
                        else
                        {
                            if (startTag != "")
                            {
                                bool writeFlag = false;
                                int j = 0;
                                foreach (var itemTag in itemChannel.Equips[equipFullName].Groups[groupFullName].Tags.Values)
                                {
                                    if (itemTag.Name == startTag)
                                    {
                                        writeFlag = true;
                                    }
                                    if (writeFlag)
                                    {
                                        if (!ArrayEnough(values, j))
                                        {
                                            break;
                                        }
                                        operateResult.IsSuccess = true;
                                        switch (itemTag.ReadWrite)
                                        {
                                            case ReadWrite.Read:
                                                operateResult.IsSuccess = false;
                                                operateResult.Message += $"{itemTag.FullName} 不允许写";
                                                readResult.IsSuccess = operateResult.IsSuccess;
                                                readResult.Message = operateResult.Message;
                                                break;
                                            default:
                                                itemTag.RealValue = values[j];
                                                break;
                                        }
                                        j++;
                                    }
                                }
                            }
                            else
                            {
                                int j = 0;
                                foreach (var itemTag in itemChannel.Equips[equipFullName].Groups[groupFullName].Tags.Values)
                                {
                                    if (!ArrayEnough(values, j))
                                    {
                                        break;
                                    }
                                    operateResult.IsSuccess = true;
                                    switch (itemTag.ReadWrite)
                                    {
                                        case ReadWrite.Read:
                                            operateResult.IsSuccess = false;
                                            operateResult.Message += $"{itemTag.FullName} 不允许写";
                                            readResult.IsSuccess = operateResult.IsSuccess;
                                            readResult.Message = operateResult.Message;
                                            break;
                                        default:
                                            itemTag.RealValue = values[j];
                                            break;
                                    }
                                    j++;
                                }
                            }
                        }
                        break;
                    }
                }
                return JsonConvert.SerializeObject(readResult);
            }
            catch (Exception ex)
            {
                operateResult.IsSuccess = false;
                operateResult.Message = ex.Message;
                readResult.IsSuccess = operateResult.IsSuccess;
                readResult.Message = operateResult.Message;
            }
            return JsonConvert.SerializeObject(readResult);
        }

        /// <summary>
        /// 单点写
        /// </summary>
        /// <param name="channelName"></param>
        /// <param name="equipName"></param>
        /// <param name="tagName"></param>
        /// <param name="values"></param>
        /// <param name="groupName"></param>
        /// <returns></returns>
        public override string WriteJson(string channelName, string equipName, string tagName, string values, string groupName = "")
        {
            base.WriteJson(channelName, equipName, tagName, values, groupName);
            string equipFullName = GetEquipFullName(channelName, equipName);
            string groupFullName = GetGroupFullName(channelName, equipName, groupName);
            OperateResult operateResult = new OperateResult();
            operateResult.IsSuccess = false;
            operateResult.Message = "未找到点";
            ReadResult readResult = new ReadResult();
            readResult.IsSuccess = false;
            readResult.Message = "未找到标签";
            try
            {
                foreach (var itemChannel in EquipChannels.Instance.EquipChannelList)
                {
                    if (itemChannel.ChannelName == channelName)
                    {
                        if (!JudgeHaveEquipOrGroup(itemChannel, equipName, groupName, out operateResult))
                        {
                            readResult.IsSuccess = operateResult.IsSuccess;
                            readResult.Message = operateResult.Message;
                            return JsonConvert.SerializeObject(readResult);
                        }
                        if (groupName == "")
                        {
                            foreach (var item in itemChannel.Equips[equipFullName].Tags.Values)
                            {
                                if (item.Name == tagName)
                                {
                                    switch (item.ReadWrite)
                                    {
                                        case ReadWrite.Read:
                                            operateResult.IsSuccess = false;
                                            operateResult.Message = "不允许写";
                                            readResult.IsSuccess = operateResult.IsSuccess;
                                            readResult.Message = operateResult.Message;
                                            return JsonConvert.SerializeObject(readResult);
                                    }
                                    item.RealValue = values;
                                    operateResult.IsSuccess = true;
                                    operateResult.Message = "成功";
                                    readResult.IsSuccess = operateResult.IsSuccess;
                                    readResult.Message = operateResult.Message;
                                    return JsonConvert.SerializeObject(readResult);
                                }
                            }
                            break;
                        }
                        else
                        {
                            foreach (var item in itemChannel.Equips[equipFullName].Groups[groupFullName].Tags.Values)
                            {
                                if (item.Name == tagName)
                                {
                                    switch (item.ReadWrite)
                                    {
                                        case ReadWrite.Read:
                                            operateResult.IsSuccess = false;
                                            operateResult.Message = "不允许写";
                                            readResult.IsSuccess = operateResult.IsSuccess;
                                            readResult.Message = operateResult.Message;
                                            return JsonConvert.SerializeObject(readResult);
                                    }
                                    item.RealValue = values;
                                    operateResult.IsSuccess = true;
                                    operateResult.Message = "成功";
                                    readResult.IsSuccess = operateResult.IsSuccess;
                                    readResult.Message = operateResult.Message;
                                    return JsonConvert.SerializeObject(readResult);
                                }
                            }
                            break;
                        }
                    }
                }

                operateResult.IsSuccess = false;
                operateResult.Message = "未找到点";
                readResult.IsSuccess = operateResult.IsSuccess;
                readResult.Message = operateResult.Message;
            }
            catch (Exception ex)
            {
                operateResult.IsSuccess = false;
                operateResult.Message = ex.Message;
                readResult.IsSuccess = operateResult.IsSuccess;
                readResult.Message = operateResult.Message;
            }
            return JsonConvert.SerializeObject(readResult);
        }

        /// <summary>
        /// 按组写
        /// </summary>
        /// <param name="channelName"></param>
        /// <param name="equipName"></param>
        /// <param name="json">json格式的值 格式为["test1","test2"]</param>
        /// <param name="startTag"></param>
        /// <param name="groupName"></param>
        /// <returns></returns>
        public override string WriteByJson(string channelName, string equipName, string json, string startTag = "", string groupName = "")
        {
            base.WriteByJson(channelName, equipName, json, startTag, groupName);
            OperateResult operateResult = new OperateResult();
            operateResult.IsSuccess = false;
            operateResult.Message = "未找到点";
            string equipFullName = GetEquipFullName(channelName, equipName);
            string groupFullName = GetGroupFullName(channelName, equipName, groupName);
            ReadResult readResult = new ReadResult();
            readResult.IsSuccess = false;
            readResult.Message = "未找到标签";
            List<string> list = new List<string>();
            try
            {
                list = (List<string>)JsonConvert.DeserializeObject(json, typeof(List<string>));
            }
            catch(Exception ex)
            {
                readResult.IsSuccess = false;
                readResult.Message = "数据转换失败。请检查json格式是否为:[\"test1\",\"test2\"]";
                return JsonConvert.SerializeObject(readResult);
            }
            string[] values = new string[list.Count];
            for (int i = 0;i<list.Count;i++)
            {
                values[i] = list[i];
            }
            try
            {
                foreach (var itemChannel in EquipChannels.Instance.EquipChannelList)
                {
                    if (itemChannel.ChannelName == channelName)
                    {
                        if (!JudgeHaveEquipOrGroup(itemChannel, equipName, groupName, out operateResult))
                        {
                            readResult.IsSuccess = operateResult.IsSuccess;
                            readResult.Message = operateResult.Message;
                            return JsonConvert.SerializeObject(readResult);
                        }
                        if (groupName == "")
                        {
                            if (startTag != "")
                            {
                                bool writeFlag = false;
                                int j = 0;
                                foreach (var itemTag in itemChannel.Equips[equipFullName].Tags.Values)
                                {
                                    if (itemTag.Name == startTag)
                                    {
                                        writeFlag = true;
                                    }
                                    if (writeFlag)
                                    {
                                        if (!ArrayEnough(values, j))
                                        {
                                            break;
                                        }
                                        operateResult.IsSuccess = true;
                                        switch (itemTag.ReadWrite)
                                        {
                                            case ReadWrite.Read:
                                                operateResult.IsSuccess = false;
                                                operateResult.Message += $"{itemTag.FullName} 不允许写";
                                                readResult.IsSuccess = operateResult.IsSuccess;
                                                readResult.Message = operateResult.Message;
                                                break;
                                            default:
                                                itemTag.RealValue = values[j];
                                                break;
                                        }
                                        j++;
                                    }
                                }
                            }
                            else
                            {
                                int j = 0;
                                foreach (var itemTag in itemChannel.Equips[equipFullName].Tags.Values)
                                {
                                    if (!ArrayEnough(values, j))
                                    {
                                        break;
                                    }
                                    operateResult.IsSuccess = true;
                                    switch (itemTag.ReadWrite)
                                    {
                                        case ReadWrite.Read:
                                            operateResult.IsSuccess = false;
                                            operateResult.Message += $"{itemTag.FullName} 不允许写";
                                            readResult.IsSuccess = operateResult.IsSuccess;
                                            readResult.Message = operateResult.Message;
                                            break;
                                        default:
                                            itemTag.RealValue = values[j];
                                            break;
                                    }
                                    j++;
                                }
                            }
                        }
                        else
                        {
                            if (startTag != "")
                            {
                                bool writeFlag = false;
                                int j = 0;
                                foreach (var itemTag in itemChannel.Equips[equipFullName].Groups[groupFullName].Tags.Values)
                                {
                                    if (itemTag.Name == startTag)
                                    {
                                        writeFlag = true;
                                    }
                                    if (writeFlag)
                                    {
                                        if (!ArrayEnough(values, j))
                                        {
                                            break;
                                        }
                                        operateResult.IsSuccess = true;
                                        switch (itemTag.ReadWrite)
                                        {
                                            case ReadWrite.Read:
                                                operateResult.IsSuccess = false;
                                                operateResult.Message += $"{itemTag.FullName} 不允许写";
                                                readResult.IsSuccess = operateResult.IsSuccess;
                                                readResult.Message = operateResult.Message;
                                                break;
                                            default:
                                                itemTag.RealValue = values[j];
                                                break;
                                        }
                                        j++;
                                    }
                                }
                            }
                            else
                            {
                                int j = 0;
                                foreach (var itemTag in itemChannel.Equips[equipFullName].Groups[groupFullName].Tags.Values)
                                {
                                    if (!ArrayEnough(values, j))
                                    {
                                        break;
                                    }
                                    operateResult.IsSuccess = true;
                                    switch (itemTag.ReadWrite)
                                    {
                                        case ReadWrite.Read:
                                            operateResult.IsSuccess = false;
                                            operateResult.Message += $"{itemTag.FullName} 不允许写";
                                            readResult.IsSuccess = operateResult.IsSuccess;
                                            readResult.Message = operateResult.Message;
                                            break;
                                        default:
                                            itemTag.RealValue = values[j];
                                            break;
                                    }
                                    j++;
                                }
                            }
                        }
                        break;
                    }
                }
                return JsonConvert.SerializeObject(readResult);
            }
            catch (Exception ex)
            {
                operateResult.IsSuccess = false;
                operateResult.Message = ex.Message;
                readResult.IsSuccess = operateResult.IsSuccess;
                readResult.Message = operateResult.Message;
            }
            return JsonConvert.SerializeObject(readResult);
        }
        #endregion

        #region 判断数组是否够长
        public bool ArrayEnough(string[] strings,int length)
        {
            if(strings.Length > length)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 判断有无设备或组
        /// <summary>
        /// 判断有无设备或组
        /// </summary>
        /// <param name="equipChannel"></param>
        /// <param name="equipFullName"></param>
        /// <param name="groupName"></param>
        /// <param name=""></param>
        /// <returns></returns>
        public bool JudgeHaveEquipOrGroup(EquipChannel equipChannel,string equipName,string groupName,out OperateResult operateResult)
        {
            operateResult = new OperateResult();
            operateResult.IsSuccess=true;
            operateResult.Message = "存在";

            bool b1 = false;
            Profinet.Equip equip = null;
            foreach (var item in equipChannel.Equips.Values)
            {
                if(item.Name == equipName)
                {
                    b1 = true;
                    equip = item;
                    break;
                }
            }
            if (!b1)
            {
                operateResult.IsSuccess = false;
                operateResult.Message = $"未找到 {equipName} 设备";
                return false;
            }
            if (groupName != "")
            {
                bool b2 = false;
                foreach (var item in equip.Groups.Values)
                {
                    if(item.Name == groupName)
                    {
                        b2 = true;
                        break;
                    }
                }
                if (!b2)
                {
                    operateResult.IsSuccess = false;
                    operateResult.Message = $"未找到 {groupName} 组";
                    return false;
                }
            }
            return true;
        }
        #endregion
    }
}
